// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProducerAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Runtime
{
    using System.Runtime.Serialization;

    /// <summary>
    /// Base class for agents that publish messages. 
    /// Inherit from this class if you are implementing a publishing agent that runs on its own schedule (e.g. I/O completion thread, timer, or unmanaged code callback).
    /// </summary>
    /// <remarks>The mere presence of the IMessagePublisher interface is sufficient to enable publishing (via this.AgentHost.Post()).</remarks>
    /// <typeparam name="TOutputMessage">The type of messages published by this agent.</typeparam>
    [DataContract]
    public abstract class ProducerAgent<TOutputMessage> : Agent, IMessagePublisher<TOutputMessage>
        where TOutputMessage : AgentMessage
    {
        /// <summary>
        /// A forwarder that can be used to broadcast our state.
        /// </summary>
        private IForwarder<TOutputMessage> publisher;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProducerAgent{TOutputMessage}"/> class. 
        /// Initializes a new instance of the ProducerAgent class.
        /// Empty constructor, to allow derived classes to have one as well.
        /// </summary>
        public ProducerAgent()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProducerAgent{TOutputMessage}"/> class. 
        /// Initializes a new instance of the ProducerAgent class.
        /// This constructor is not called when the agent is created from a manifest file.
        /// </summary>
        /// <param name="name">
        /// A friendly name for this agent instance.
        /// </param>
        public ProducerAgent(string name)
            : base(name)
        {
        }

        /// <summary>
        /// Gets a forwarder that can be used to broadcast our state.
        /// </summary>
        protected IForwarder<TOutputMessage> Publisher
        {
            get
            {
                return this.publisher;
            }
        }

        /// <summary>
        /// Initializer that provides a way for us to broadcast messages.
        /// </summary>
        /// <param name="forwarder">A forwarder that can be used to broadcast our state.</param>
        public void InitializePublisher(IForwarder<TOutputMessage> forwarder)
        {
            this.publisher = forwarder;
        }
    }
}
